package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.bean.ServiceConstant;
import cn.nubia.admin.application.bean.TopicSoftMap;
import cn.nubia.admin.application.service.TopicSoftMapService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.application.bean.Topic;
import cn.nubia.admin.application.bean.TopicVo;
import cn.nubia.admin.application.query.TopicQueryParam;
import cn.nubia.admin.application.service.TopicService;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.springmvc.Response;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 专题页面控制器.
 *
 * @author cmJason
 */
@Controller
@RequestMapping(value = "/admin/topic")
public class TopicController {

    @Autowired
    private TopicService topicService;
    @Autowired
    private TopicSoftMapService topicSoftMapService;


    /***
     * 草稿列表
     *
     * @return
     */
    @RequiresPermissions(value = "soft:rec:topicList")
    @RequestMapping(value = "topicList", method = RequestMethod.GET)
    public ModelAndView topicList(TopicQueryParam topicQuery) {
        ModelAndView modelAndView = new ModelAndView("/topic/topicList");
        Paged<TopicVo> topicPaged = topicService.findPageList(topicQuery);
        modelAndView.addObject("query", topicQuery);
        modelAndView.addObject("topicPage", topicPaged);
        return modelAndView;
    }

    @RequestMapping(value = "viewTopic", method = RequestMethod.GET)
     public ModelAndView viewTopic(Integer topicId) {
        ModelAndView modelAndView = new ModelAndView("/topic/topicForm");
        Topic topic = new Topic();
        List<TopicSoftMap> topicSoftMaps = new ArrayList<>();
        if (topicId != null) {
            topic = topicService.get(topicId);
            topicSoftMaps = topicSoftMapService.listTopicSoftMaps(topicId);
        }

        modelAndView.addObject("topic", topic);
        modelAndView.addObject("topicId", topicId);
        modelAndView.addObject("softMaps",topicSoftMaps);

        modelAndView.addObject("showTypes", ServiceConstant.showTypes);
        return modelAndView;
    }

    @RequestMapping(value = "viewTopicSoftMap", method = RequestMethod.GET)
    public ModelAndView viewTopicSoftMap(Integer id,Integer topicId) {
        ModelAndView modelAndView = new ModelAndView("/topic/choiceSoft");
        Topic topic = new Topic();
        TopicSoftMap topicSoftMap = new TopicSoftMap();

        if(id!=null){
            topicSoftMap = topicSoftMapService.get(id);
            topic = topicService.get(topicSoftMap.getTopicId());
        }else{
            if(topicId!=null)topic = topicService.get(topicId);
        }
        modelAndView.addObject("topic", topic);
        modelAndView.addObject("softMap",topicSoftMap);
        return modelAndView;
    }

    @RequestMapping(value = "listTopicSoftMap", method = RequestMethod.GET)
    public ModelAndView listTopicSoftMap(Integer topicId) {
        ModelAndView modelAndView = new ModelAndView("/topic/softmaplist");
        Topic topic = new Topic();
        TopicSoftMap topicSoftMap = new TopicSoftMap();
        List<TopicSoftMap> topicSoftMaps = new ArrayList<>();
        if (topicId != null) {
            topic = topicService.get(topicId);
            topicSoftMaps = topicSoftMapService.listTopicSoftMaps(topicId);
        }
        modelAndView.addObject("topic", topic);
        modelAndView.addObject("softMaps",topicSoftMaps);
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping(value = "getTopicList", method = RequestMethod.GET)
    public Paged<TopicVo> getTopicList(TopicQueryParam queryParam) {
        Paged<TopicVo> topicPaged = topicService.findPageList(queryParam);
        return topicPaged;
    }

    @ResponseBody
    @RequestMapping(value = "saveTopic",method = RequestMethod.POST)
    public Response<Boolean> saveTopic(Topic topic) {
        topicService.save(topic);
        return Response.getSuccessResponse(true);
    }


    @ResponseBody
    @RequestMapping(value = "deleteTopic")
    public Response<Boolean> deleteTopic(Integer topicId){
        topicService.delete(topicId);
        return Response.getSuccessResponse(true);
    }



    @ResponseBody
    @RequestMapping(value = "saveSoftMap")
    public Response<Boolean> saveSoftMap(TopicSoftMap softMap){
        boolean result = topicSoftMapService.saveSoftMap(softMap);
        return Response.getSuccessResponse(result);
    }
    @ResponseBody
    @RequestMapping(value = "deleteSoftMap")
    public Response<Boolean> deleteSoftMap(Integer id){
        boolean result = topicSoftMapService.deleteSoftMap(id);
        return Response.getSuccessResponse(result);
    }


}
